package com.vsd.mobile;





import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.demo.annotation.LoginFilter;
import com.demo.annotation.MobileMethod;
import com.demo.mobile.MobException;
import com.demo.mobile.MobileInfo;
import com.demo.util.DateUtil;
import com.demo.util.ProperityUtil;
import com.demo.validator.MobileLoginValidate;
import com.shopcart.domain.CreditCategory;
import com.vsd.domain.CreditGoods;
import com.vsd.domain.User;
import com.vsd.domain.UserCreditLog;
import com.vsd.service.ChangeLogService;
import com.vsd.service.CreditCategoryService;
import com.vsd.service.CreditGoodsService;
import com.vsd.service.UserCreditLogService;
import com.vsd.service.UserService;
import com.zy.credit.proto.MOCredit.MCreditGoods;
import com.zy.credit.proto.MOCredit.MCreditGoodsList;
import com.zy.credit.proto.MOCredit.MCreditLog;
import com.zy.credit.proto.MOCredit.MCreditLogList;
import com.zy.index.proto.MOIndex.MCategoryList;
import com.zy.index.proto.MOIndex.MStoreCategory;
import com.zy.sys.proto.MOCommon.MRet;

@Controller
@RequestMapping("/mobile/")
public class MCreditController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private UserCreditLogService userCreditLogService;
	@Autowired
	private CreditCategoryService creditCategoryService;
	@Autowired
	private CreditGoodsService creditGoodsService;
	@Autowired
	private ChangeLogService changeLogService;
	@Autowired
	private ProperityUtil properityUtil;
	
	/**
	@api mMyCredit mMyCredit	//我的积分（登录）
	@return MRet //msg 剩余积分数 
	*/
	@RequestMapping("/mMyCredit")
	@MobileMethod
	@LoginFilter(validateClz=MobileLoginValidate.class)
	public MRet.Builder mMyCredit(MobileInfo mobileInfo) throws Exception {
		MRet.Builder ret = MRet.newBuilder();
		User u = userService.findByPrimaryKey(mobileInfo.getUserid());
		ret.setCode(0);
		ret.setMsg(u.getCredit().toString());
		return ret;
	}
	

	/**
	@api mCreditLog mCreditLog	//积分日志（登录 分页）
	@return MCreditLogList  
	*/
	@RequestMapping("/mCreditLog")
	@MobileMethod(isPage=true)
	@LoginFilter(validateClz=MobileLoginValidate.class)
	public MCreditLogList.Builder mCreditLog(MobileInfo mobileInfo,Integer page,Integer limit) throws Exception {
		MCreditLogList.Builder ret = MCreditLogList.newBuilder();
		
		Page<UserCreditLog> list = userCreditLogService.findByUserId(mobileInfo.getUserid(), 
				new PageRequest(page, limit,new Sort(Direction.DESC, "createTime")));
		
		for (UserCreditLog l : list) {
			MCreditLog.Builder mini =  MCreditLog.newBuilder();
			mini.setCredit(l.getNum());
			mini.setItem(l.getItem());
			mini.setTime(DateUtil.dateToStr(l.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
			mini.setType(l.getType());
			ret.addList(mini);
		}
		
		return ret;
	}
	
	/**
	@api mCreditCategory mCreditCategory	//积分商城分类
	@return MCategoryList 
	*/
	@RequestMapping("/mCreditCategory")
	@MobileMethod
	public MCategoryList.Builder mCreditCategory(MobileInfo mobileInfo) throws Exception {
		MCategoryList.Builder ret = MCategoryList.newBuilder();
		Collection<CreditCategory> list = creditCategoryService.findAllByOrderBySortDesc();
		for (CreditCategory cate : list) {
			MStoreCategory.Builder c = MStoreCategory.newBuilder();
			c.setTitle(cate.getName());
			c.setId(cate.getCode());
			ret.addList(c);
		}
		return ret;
	}
	
	/**
	@api mCreditGoodsList mCreditGoodsList	//积分商品 (分页)
	@param required string code //分类(不传时返回全部)
	@return MCreditGoodsList 
	*/
	@RequestMapping("/mCreditGoodsList")
	@MobileMethod(isPage=true)
	public MCreditGoodsList.Builder mCreditGoodsList(MobileInfo mobileInfo,String code,Integer page,Integer limit) throws Exception {
		MCreditGoodsList.Builder ret = MCreditGoodsList.newBuilder();
		Page<CreditGoods> list = null;
		if(StringUtils.isBlank(code)){
			list = creditGoodsService.findAll(new PageRequest(page, limit,new Sort(Direction.DESC, "createTime")));
		}else{
			list = creditGoodsService.findByCateCode(code,new PageRequest(page, limit,new Sort(Direction.DESC, "createTime")));
		}
		User u = null;
		if(StringUtils.isNotBlank(mobileInfo.getUserid()) ) u= userService.findByPrimaryKey(mobileInfo.getUserid());
		
		
		
		for (CreditGoods g : list) {
			MCreditGoods.Builder mini = MCreditGoods.newBuilder();
			mini.setContent(properityUtil.getSelf_url()+"/detail?id="+g.getId()+"&type=3");
			mini.setCredit(g.getNum());
			mini.setId(g.getId());
			mini.setImg(g.getImg());
			mini.setImgs(g.getImgs());
			mini.setInfo(g.getInfo());
			mini.setName(g.getTitle());
			if(u!=null){
				 mini.setState(g.getNum()>u.getCredit()?0:1);
			}else{
				 mini.setState(1);
			}
			ret.addList(mini);
		}
		return ret;
	}
	
	/**
	@api mChangeCreditGoods mChangeCreditGoods	//兑换礼品
	@return MRet 
	*/
	@RequestMapping("/mChangeCreditGoods")
	@MobileMethod
	@LoginFilter(validateClz=MobileLoginValidate.class)
	public MRet.Builder mChangeCreditGoods(MobileInfo mobileInfo,String id) throws Exception {
		if(StringUtils.isBlank(id)) throw new MobException("id");
		
		MRet.Builder ret = MRet.newBuilder();
		//更新用户积分 生成积分日志 更新库存
		creditGoodsService.changeCreditGoods(mobileInfo,id);
		
		ret.setCode(0);
		
		return ret;
	}
	

	/**
	@api mMyCreditGoods mMyCreditGoods	//我的礼品
	@return MCreditGoodsList 
	*/
	@RequestMapping("/mMyCreditGoods")
	@MobileMethod(isPage=true)
	@LoginFilter(validateClz=MobileLoginValidate.class)
	public MCreditGoodsList.Builder mMyCreditGoods(MobileInfo mobileInfo,Integer page,Integer limit) throws Exception {
		return changeLogService.findByUserId(mobileInfo.getUserid(), page, limit);
	}
	 
}
